# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: I_λ.py
  @time: 2022/3/19 14:38
  @desc:电流随波长的变化
"""
import time
from ATS.hardwarescan import conf_file_manager
from ATS.application.LCR_App.U2722A_App import U2722A_App
from ATS.application.LASER_App.L8164B_App import L8164B_App
import matplotlib.pyplot as plt
from ATS.datamanager import data_transfer_station as DataTransferStation
from ATS.switchdriver.LightSwitchDriver import LightSwitchDriver
from collections import namedtuple
from utils.log_util import loggings


# def measurement_test(u2722_channel=1, i_range="1mA", i_limit=0.001, voltage=3, i_nplc=1, l8164_power=5, start_wave=1243,
#                      stop_wave=1380, wave_point=50):
def measurement_test(para):
    result_data = namedtuple("result_data", "subdie,test_item,test_result")
    light_inst = LightSwitchDriver.instance()
    if light_inst.is_connected:
        light_inst.set_all_switch("01", "05")
        time.sleep(1)
        loggings.info("switch" + str(light_inst.query_switch()))
    else:
        light_inst.connect()
    subdie = para.subdie
    if len(para.smu_setting) == 1:
        smu_setting = para.smu_setting[0]
        smu_name = smu_setting["smu"]
        smu_inst = U2722A_App(smu_name)
        smu_inst_channel = smu_setting["channel"]
        smu_inst_voltage = smu_setting["voltage_list"][0]
        smu_inst_voltage_range = smu_setting["voltage_range"]
        smu_inst_nplc = smu_setting["nplc"]
        smu_inst_current_range = smu_setting["current_range"]
        smu_inst_current_limit = smu_setting["current_limit"]

    l8164b_app = L8164B_App(para.l8164_setting["title"])
    powervalue = para.l8164_setting["power_value"]
    wave_list = para.l8164_setting["wave_length"]

    smu_inst.set_voltage_range(smu_inst_channel)
    smu_inst.set_current_range_limit(smu_inst_current_range, smu_inst_current_limit, smu_inst_channel)
    smu_inst.set_current_nplc(smu_inst_nplc, smu_inst_channel)
    smu_inst.set_voltage(smu_inst_voltage, smu_inst_channel)
    smu_inst.output_state(1, smu_inst_channel)
    l8164b_app.set_power_value(powervalue)
    l8164b_app.output_on()
    current_data = []
    for wave in wave_list:
        l8164b_app.set_wave_length(wave)
        current = smu_inst.get_current_value(smu_inst_channel)
        current_data.append(float(current))
    smu_inst.output_state(0, smu_inst_channel)
    DataTransferStation.get_DTS().postarraydata('test',
                                                result_data(para.subdie, para.test_item, (wave_list, current_data)))
    # return wave_list, current_data


# def plot_curve(x_data, y_data):
#     plt.figure(figsize=(20, 8), dpi=80)
#     plt.yscale('log')
#     fig, ax = plt.subplots()
#     ax.set_yscale('log')
#     ax.plot(x_data, y_data, "b", label="1")
#     ax.set_title("I-λ Curve")
#     ax.set_xlabel("WaveLength")
#     ax.set_ylabel("Value")
#     ax.grid(True)
#     ax.legend()
#     plt.show()


print("Iλ", locals()["para"])
measurement_test(locals()["para"])
# plot_curve(x_data, y_data)
